package com.liang.cloud.Controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liang.cloud.Emun.ResultEnum;
import com.liang.cloud.Entity.Cart;
import com.liang.cloud.Entity.ProductInfo;
import com.liang.cloud.Exception.SellException;
import com.liang.cloud.Form.CartForm;
import com.liang.cloud.Handler.SellFackHandler;
import com.liang.cloud.Util.ResultVoUtil;
import com.liang.cloud.Vo.ResultVo;
import com.liang.cloud.Feign.ProductFeign;
import com.liang.cloud.Service.CartService;
import com.liang.cloud.Vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author: 余亮亮
 * @description:
 * @create: 2020-03-26 13:07:19
 **/
@RestController
@Slf4j
@RequestMapping("/cart/buyer")
public class BuyerCartController {

    @Autowired
    CartService cartService;
    @Autowired
    ProductFeign productFeign;

    @PostMapping("/add")
    public ResultVo addCart(@RequestBody Cart cart){
        Cart cart1 = cartService.addCart(cart);
        return ResultVoUtil.success();
    }

    @GetMapping("/list")
    @SentinelResource(value = "cartBuyerList",fallback = "CallTimeOneArg",fallbackClass = SellFackHandler.class)
    public ResultVo<List<CartVo>> list(String openid){
        List<Cart> cartList = cartService.findByOpenid(openid);
        List<CartVo> cartVoList = new ArrayList<>();
        for (Cart cart : cartList){
            CartVo cartVo = new CartVo();
            ProductInfo productInfo = productFeign.findOne(cart.getProductId());
            BeanUtils.copyProperties(productInfo,cartVo);
            cartVo.setQuantity(cart.getQuantity());
            cartVo.setCartId(cart.getCartId());
            cartVoList.add(cartVo);
        }
        return ResultVoUtil.success(cartVoList);
    }

    @PostMapping("/update")
    public ResultVo update(@RequestBody HashMap<String,String> map){
        Gson gson = new Gson();
        List<CartForm> cartVoList = new ArrayList<>();
        try {
            //List<Object> obj=  gson.fromJson(字符串, new TypeToken<List<要转换的list集合的>>(){}.getType());
            cartVoList = gson.fromJson(map.get("carts"),
                    new TypeToken<List<CartForm>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", map.get("carts"));
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        for (CartForm cartForm : cartVoList){
            Cart cart = new Cart();
            cart.setCartId(cartForm.getCartId());
            cart.setProductId(cartForm.getId());
            cart.setQuantity(cartForm.getNum());
            cart.setOpenid(map.get("openid"));
            cartService.sava(cart);
        }
        return ResultVoUtil.success();
    }

    @PostMapping("/delete")
    public ResultVo delete(@RequestBody HashMap<String,String> map){
        Integer cartId = Integer.valueOf(map.get("cartId"));
        String openid = map.get("openid");
        if (cartId.toString().isEmpty()||openid.isEmpty()){
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        cartService.delete(Arrays.asList(cartId));
        return ResultVoUtil.success();
    }



}
